package br.com.amadeus.android.adapters;

import java.util.List;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import br.com.amadeus.android.activities.home.R;
import br.com.amadeus.android.business.FacadeAmadeusDroid;
import br.com.amadeus.android.entities.Goal;
import br.com.amadeus.android.entities.Task;
import br.com.amadeus.android.util.SchedulerSyncronize;

public class TaskAdapter extends BaseAdapter {
	protected static final String CATEGORIA = "Task";
	private Context context;
	private List<TaskList> tasks;
	FacadeAmadeusDroid facade;
	AlarmManager alarme;
	int goal_id;

	public TaskAdapter(Context context, List<TaskList> tasks, AlarmManager alarme, int goal_id) {
		super();
		this.context = context;
		this.tasks = tasks;
		this.facade = FacadeAmadeusDroid.getInstance();
		this.alarme = alarme;
		this.goal_id = goal_id;
	}

	public int getCount() {
		return tasks.size();
	}

	public Object getItem(int pos) {
		return tasks.get(pos);
	}

	public long getItemId(int pos) {
		return tasks.get(pos).getId();
	}

	public View getView(int pos, View view, ViewGroup viewGroup) {
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.amadeus_task_item, null);
		
		//atribuindo a objetivo o objeto do arraylist da posição position
		final TaskList task = tasks.get(pos); 
		
		TextView tvName = (TextView) v.findViewById(R.id.tvName);
		tvName.setText(task.getName());
		ImageButton imgEditar = (ImageButton) v.findViewById(R.id.imgEditTask);
		ImageButton imgExcluir = (ImageButton) v.findViewById(R.id.imgDeleteTask);
		
		imgEditar.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v) 
			{
				SchedulerSyncronize.sendBroadcast(1, context, alarme, "RECEIVER_FORMTASK",(int)task.getId());
			}
		});
		
		imgExcluir.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v) 
			{
				AlertDialog.Builder dialog = new AlertDialog.Builder(context);
				dialog.setMessage("Tem certeza que deseja excluir esse registro?");
				
				dialog.setNegativeButton("Não", new DialogInterface.OnClickListener() 
				{
					public void onClick(DialogInterface dialog, int which) 
					{
						
					}
				});

				dialog.setPositiveButton("Sim", new DialogInterface.OnClickListener() 
				{
					public void onClick(DialogInterface dialog, int which) 
					{
						facade.deleteTask((int) task.getId());
						SchedulerSyncronize.sendBroadcast(1, context, alarme, "RECEIVER_HOMETASK", goal_id);
					}
				});
				
				dialog.show();
			}
		});
		
		return v;
	}
	
}